import { createRouter, createWebHashHistory } from 'vue-router'
import store from '../store/index.js'

const routes = [
  {
    path: '/',
    name: 'Index',
    meta: {
      title: '商城-首页',
      requiresAuth: false
    },
    component: () => import('@/views/FrontIndex.vue')
  },
  {
    path: '/commlist',
    name: 'CommList',
    meta: {
      title: '商城-商品列表',
      requiresAuth: true
    },
    component: () => import('@/views/CommList.vue')
  },
  {
    path: '/productDetail/:id',
    name: 'ProductDetail',
    meta: {
      title: '商城-商品详情',
      requiresAuth: true
    },
    component: () => import('@/views/ProductDetail.vue')
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '商城-登录',
      requiresAuth: false
    },
    component: () => import('@/views/IndexLogin.vue')
  },
  {
    path: '/register',
    name: 'register',
    meta: {
      title: '商城-注册',
      requiresAuth: false
    },
    component: () => import('@/views/IndexReg.vue')
  },
  {
    path: '/shopping-cart',
    name: 'ShoppingCart',
    meta: {
      title: '商城-购物车',
      requiresAuth: true
    },
    component: () => import('@/views/ShoppingCart.vue')
  },
  {
    path: '/order-list',
    name: 'UserOrderList',
    meta: {
      title: '商城-我的订单',
      requiresAuth: true
    },
    component: () => import('@/views/UserOrderList.vue')
  },
  {
    path: '/admin/login',
    name: 'AdminLogin',
    meta: {
      title: '后台管理系统-登录',
      requiresAdminAuth: false
    },
    component: () => import('@/views/admin/Login.vue')
  },
  {
    path: '/admin',
    name: 'Dashboard',
    meta: {
      title: '首页-后台管理系统',
      requiresAdminAuth: true
    },
    component: () => import('@/views/admin/Dashboard.vue'),
    redirect: 'admin/home',
    children: [
      {
        path: 'home',
        name: 'AdminHome',
        meta: {
          title: '首页-后台管理系统',
          requiresAdminAuth: true
        },
        component: () => import('@/views/admin/Home.vue')
      },
      {
        path: 'userlist',
        name: 'UserList',
        meta: {
          title: '用户页-后台管理系统',
          requiresAdminAuth: true
        },
        component: () => import('@/views/admin/User.vue')
      },
      {
        path: 'community',
        name: 'Community',
        meta: {
          title: '商品页-后台管理系统',
          requiresAdminAuth: true
        },
        component: () => import('@/views/admin/Community.vue')
      },
      {
        path: 'banner',
        name: 'Banner',
        meta: {
          title: '轮播图-后台管理系统',
          requiresAdminAuth: true
        },
        component: () => import('@/views/admin/Banner.vue')
      }
      ,
      {
        path: 'orderlist',
        name: 'OrderList',
        meta: {
          title: '订单-后台管理系统',
          requiresAdminAuth: true
        },
        component: () => import('@/views/admin/OrderList.vue')
      }
    ]
  }
]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  document.title = to.meta.title ? to.meta.title : '商城'
  if (to.meta.requiresAuth) {
    // 前台需要登录
    if (!store.state.uInfo.userInfo.username) {
      // 前台用户未登录
      next('/login') // 重定向到登录页
    } else {
      next() // 继续导航
    }
  } else if (to.meta.requiresAdminAuth) {
    // 后台管理员需要登录
    if (!store.state.SysuInfo.userInfo.username) {
      // 后台管理员未登录
      next('/admin/login') // 重定向到后台登录页
    } else {
      next() // 继续导航
    }
  }else{
    next()
  }
})
export default router
